package com.xdxc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xdxc.model.entity.BankQuestion;
import com.xdxc.model.entity.QuestionBank;
import com.xdxc.response.Result;
import com.xdxc.service.BankQuestionService;
import com.xdxc.service.QuestionBankService;
import com.xdxc.service.QuestionService;
import com.xdxc.vo.QuestionWithOptionsVo;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/questionBank")
public class QuestionBankController {

    private final QuestionBankService questionBankService;

        private final QuestionService questionService;
        private final BankQuestionService bankQuestionService;





    public QuestionBankController(QuestionBankService questionBankService,
                                  QuestionService questionService,
                                BankQuestionService bankQuestionService) {
        this.questionBankService = questionBankService;
        this.questionService = questionService;
        this.bankQuestionService=bankQuestionService;
    }

    // 新增题库
    @PostMapping
    public Result add(@RequestBody QuestionBank questionBank) {
        boolean success = questionBankService.save(questionBank);
        return success ? Result.success(questionBank) : Result.error("添加失败");
    }

    // 根据ID删除题库
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        boolean success = questionBankService.removeById(id);
        return success ? Result.success("") : Result.error("删除失败");
    }

    // 修改题库
    @PutMapping
    public Result update(@RequestBody QuestionBank questionBank) {
        boolean success = questionBankService.updateById(questionBank);
        return success ? Result.success(questionBank) : Result.error("更新失败");
    }

    // 根据ID查询题库
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id) {
        QuestionBank questionBank = questionBankService.getById(id);
        return questionBank != null ? Result.success(questionBank) : Result.error("未找到对应题库");
    }

    // 分页查询题库列表
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       @RequestParam(required = false) String name,
                       @RequestParam(required = false) Long subjectId) {

        Page<QuestionBank> page = new Page<>(pageNum, pageSize);
        QueryWrapper<QuestionBank> wrapper = new QueryWrapper<>();

        if (name != null && !name.isEmpty()) {
            wrapper.like("name", name);
        }

        if (subjectId != null) {
            wrapper.eq("subject_id", subjectId);
        }

        wrapper.orderByDesc("create_time");

        return Result.success(questionBankService.page(page, wrapper));
    }


    @GetMapping("/pageQuery")
    public Result QuestionBank(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       @RequestParam(required = false) Long bankId
                      ) {

        return bankQuestionService.QuestionBank(pageNum, pageSize, bankId);
    }


}
